package com.zifan.yygh.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zifan.yygh.dao.HospitalSetMapper;
import com.zifan.yygh.model.hosp.HospitalSet;
import com.zifan.yygh.service.HospitalSetService;
import com.zifan.yygh.utils.MD5;
import com.zifan.yygh.vo.hosp.HospitalSetQueryVo;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * <p>
 * 医院设置表 服务实现类
 * </p>
 *
 * @author 子凡
 * @since 2021-09-23
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    /**
     * 根据条件分页获取医院设置
     * @param current 当前页
     * @param limit 每页数量
     * @param hospitalSetQueryVo 接收参数的vo
     * @return 分页对象
     */
    @Override
    public Page findPageHospSet(long current, long limit, HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> page = new Page<>(current, limit);

        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(HospitalSet::getCreateTime);

        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();

        if (StrUtil.isNotEmpty(hoscode)) {
            wrapper.lambda().eq(HospitalSet::getHoscode, hoscode);
        }
        if (StrUtil.isNotEmpty(hosname)) {
            wrapper.lambda().like(HospitalSet::getHosname, hosname);
        }

        this.page(page, wrapper);
        return page;
    }

    /**
     * 添加医院信息
     * @param hospitalSet 医院信息
     * @return 是否成功
     */
    @Override
    public Boolean saveHospitalSet(HospitalSet hospitalSet) {
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //设置签名 规则：当前时间加100以内随机数之后MD5加密
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(
                System.currentTimeMillis() + "" + random.nextInt(1000)
        ));
        return this.save(hospitalSet);
    }
}
